"""add image tasks tables only

Revision ID: 8af636bb96d3
Revises: d585a772ecb9
Create Date: 2025-01-08 17:30:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = '8af636bb96d3'
down_revision: Union[str, None] = 'd585a772ecb9'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema - only create new image tasks tables."""
    # Create image_tasks table
    op.create_table('image_tasks',
        sa.Column('id', sa.Integer(), nullable=False, comment='任务ID'),
        sa.Column('user_id', sa.Integer(), nullable=False, comment='用户ID'),
        sa.Column('task_name', sa.String(length=200), nullable=False, comment='任务名称'),
        sa.Column('description', sa.Text(), nullable=True, comment='任务描述'),
        sa.Column('is_active', sa.Boolean(), nullable=True, comment='是否激活'),
        sa.Column('image_count', sa.Integer(), nullable=True, comment='图片数量'),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False, comment='创建时间'),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False, comment='更新时间'),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_image_tasks_id'), 'image_tasks', ['id'], unique=False)
    op.create_index(op.f('ix_image_tasks_user_id'), 'image_tasks', ['user_id'], unique=False)
    
    # Create image_task_items table
    op.create_table('image_task_items',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('task_id', sa.Integer(), nullable=False, comment='任务ID'),
        sa.Column('image_url', sa.Text(), nullable=False, comment='图片URL'),
        sa.Column('original_filename', sa.String(length=255), nullable=True, comment='原始文件名'),
        sa.Column('cos_url', sa.Text(), nullable=True, comment='COS访问URL'),
        sa.Column('cos_key', sa.String(length=500), nullable=True, comment='COS对象键'),
        sa.Column('file_size', sa.Integer(), nullable=True, comment='文件大小(字节)'),
        sa.Column('file_type', sa.String(length=50), nullable=True, comment='文件类型'),
        sa.Column('width', sa.Integer(), nullable=True, comment='图片宽度'),
        sa.Column('height', sa.Integer(), nullable=True, comment='图片高度'),
        sa.Column('upload_status', sa.String(length=50), nullable=True, comment='上传状态: pending/uploading/success/failed'),
        sa.Column('error_message', sa.Text(), nullable=True, comment='错误信息'),
        sa.Column('sort_order', sa.Integer(), nullable=True, comment='排序顺序'),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False, comment='创建时间'),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False, comment='更新时间'),
        sa.ForeignKeyConstraint(['task_id'], ['image_tasks.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_image_task_items_id'), 'image_task_items', ['id'], unique=False)
    op.create_index(op.f('ix_image_task_items_task_id'), 'image_task_items', ['task_id'], unique=False)


def downgrade() -> None:
    """Downgrade schema - drop image tasks tables."""
    op.drop_index(op.f('ix_image_task_items_task_id'), table_name='image_task_items')
    op.drop_index(op.f('ix_image_task_items_id'), table_name='image_task_items')
    op.drop_table('image_task_items')
    op.drop_index(op.f('ix_image_tasks_user_id'), table_name='image_tasks')
    op.drop_index(op.f('ix_image_tasks_id'), table_name='image_tasks')
    op.drop_table('image_tasks')